package com.ejuornal.dao.impl;

import java.lang.reflect.ParameterizedType;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Page;
import com.ejuornal.bean.vo.UserSubscibeJudge;
import com.ejuornal.dao.UserSJDao;
import com.ejuornal.springutil.SpringUtil;
@Component("userSJDaoImpl")
public class UserSJDaoImpl extends DaoSupport implements UserSJDao {



public Set<Integer>  findIdsByUserIdSet(int userId){
	
	/****/
	Set<Integer> set = new HashSet<Integer>();
	String sql = "SELECT periodicalId FROM t_subscribe WHERE userId = ?";
	List<Integer> list = getJdbcTemplate().query(sql, new Object[]{userId}, new RowMapper<Integer>(){

		@Override
		public Integer mapRow(ResultSet rSet, int num) throws SQLException {
			int userId = rSet.getInt("periodicalId");
			return userId;
		}});
	for (Integer integer : list) {
		set.add(integer);
	}
	
	return set;		
}

@Override
public Page<UserSubscibeJudge> findAllJudge(int current_record, String url,
		String hsql) {
	// TODO Auto-generated method stub
	return findAll(current_record, url, hsql);
}

public Page<UserSubscibeJudge>  findAll(int current_record, String url,String hsql){
	
	 Page<UserSubscibeJudge> page = new Page<UserSubscibeJudge>();
     int pageSize = page.getPageSize();
     String sql = "select count(*) from  t_periodical " +hsql;
     int totalRecords = this.getJdbcTemplate().queryForInt(sql);
     
     sql = "select pubDate,name,t_periodical.periodicalId,cover from  t_periodical " + hsql + " limit " + current_record + "," + pageSize;
     final List<UserSubscibeJudge> list = new ArrayList<UserSubscibeJudge>();   
      this.getJdbcTemplate().query(sql, new RowMapper<UserSubscibeJudge>(){
			@Override
			public UserSubscibeJudge mapRow(ResultSet rSet, int num) throws SQLException {
				// TODO Auto-generated method stub
				UserSubscibeJudge userSubscibeJudge = new UserSubscibeJudge();
				userSubscibeJudge.setDate(rSet.getDate("pubDate"));
				userSubscibeJudge.setName(rSet.getString("name"));
				//userSubscibeJudge.setState(state);
				userSubscibeJudge.setPeriodicalId(rSet.getInt("periodicalId"));
				userSubscibeJudge.setCover(rSet.getString("cover"));
				list.add(userSubscibeJudge);
				return userSubscibeJudge; 
			}
         });
     
     page.setUrl(url);
     page.setTotalRecords(totalRecords);
     page.setTotalPage(totalRecords);
     page.setRecords(list);
     
     return page;
	
}

public Page<UserSubscibeJudge>  findAllJudge(int current_record, String url,String hsql,int userId){
	 Page<UserSubscibeJudge> pageNew = new Page<UserSubscibeJudge>();
	 Page<UserSubscibeJudge> page = findAll(current_record, url, hsql);
	 List<UserSubscibeJudge> list = page.getRecords();
	 
	 
	 for (UserSubscibeJudge userSubscibeJudge : list) {
			System.out.print(userSubscibeJudge.getState()  + "  pp  ");
	}
	 
	 
	 Set<Integer>  set = findIdsByUserIdSet(userId);
	 for (UserSubscibeJudge userSubscibeJudge : list) {
		 int id = userSubscibeJudge.getPeriodicalId();
		if(set.contains(id)){
			/*  1 代表 登录用户已订阅   ，默认为 0代表未定约   */
			userSubscibeJudge.setState(1);
		}
	}
	 
	 for (UserSubscibeJudge userSubscibeJudge : list) {
			System.out.print(userSubscibeJudge.getState()  + "    ");
	}
	 
	 /**
	  * page.setUrl(url);
     page.setTotalRecords(totalRecords);
     page.setTotalPage(totalRecords);
     page.setRecords(list);
	  */
	 System.out.println();
	 System.out.println(page.getTotalPage());
		System.out.println(page.getTotalRecords());
		System.out.println(page.getUrl());
		 System.out.println("-----------------------");
		 System.out.println(page.getTotalPage());
	 pageNew.setRecords(list);
	 pageNew.setUrl(page.getUrl());
	 pageNew.setTotalRecords(page.getTotalRecords());
	 
	 pageNew.setTotalPage(page.getTotalRecords());
	 return pageNew;
}

	public static void main(String[] args) {
		UserSJDaoImpl userSJDaoImpl = (UserSJDaoImpl) SpringUtil.getBean("userSJDaoImpl");
		//System.out.println(userSJDaoImpl.findAll(0, ""," where categoryId = 3" ).getRecords().get(1).getName());
		//" where categoryId = ? 
		Set<Integer> set = userSJDaoImpl.findIdsByUserIdSet(4);
		System.out.println();
		for (Integer integer : set) {
			System.out.print(integer + "    ");
		}
		
		Page<UserSubscibeJudge> page =userSJDaoImpl.findAllJudge(0, ""," where categoryId = 3",4 );
		System.out.println();
		List<UserSubscibeJudge>list2 = page.getRecords();
		for (UserSubscibeJudge userSubscibeJudge : list2) {
			System.out.print(userSubscibeJudge.getState() + "    ");
		}	
	}


	
	
}
